# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, tools


class PurchaseReceiptOsync(models.Model):
    _name = 'purchase.receipt.osync'
    _auto = False

    # date = fields.Date()
    # total_timesheet = fields.Float()
    # total_attendance = fields.Float()
    # total_difference = fields.Float()
    picking_state = fields.Char()
    write_date = fields.Char()
    location_dest_id = fields.Char()
    id_picking = fields.Char()
    picking_type_id = fields.Char()
    name_picking = fields.Char()
    name_po = fields.Char()
    name_rp = fields.Char()
    code_rp = fields.Char()
    name_company = fields.Char()
    code_company = fields.Char()
    time_picking = fields.Char()
    code_user = fields.Char()
    stock_move = fields.Char()
 
    @api.model_cr
    def init(self):
        tools.drop_view_if_exists(self.env.cr,'purchase_receipt_osync')
        self._cr.execute(""" 
            CREATE OR REPLACE VIEW purchase_receipt_osync AS (
                select distinct
            m.id,
            group_concat(m.name || ':' || m.id) as stock_move,
            t.id as id_picking,
            t.picking_type_id,
            t.name as name_picking,
            s.name as name_po,
            a.name as name_rp,
            a.nc_code as code_rp,
            b.name as name_company,
            b.nc_code as code_company,
            t.create_date as time_picking,
            t.location_dest_id,
            t.state as picking_state,
            c.nc_code as code_user,
            t.write_date 
            from
            stock_move
            m left join stock_picking t on m.picking_id = t.
            id left join purchase_order s on t.origin = s.
            name left join res_partner a on s.partner_id = a.
            id left join res_company b on t.company_id = b.id 
            left join res_users c on t.write_uid = c.id 
            group by
            m.id ,
            t.id ,
            t.picking_type_id,
            t.name ,
            t.write_uid,
            s.name ,
            a.name ,
            a.nc_code,
            b.name ,
            b.nc_code ,
            t.create_date ,
            t.location_dest_id,
            t.state,
            c.nc_code,
            t.write_date
        )
        """)
